//
//  MLDSearchView.swift
//  Sina
//
//  Created by 万世豪情 on 15/10/14.
//  Copyright © 2015年 万世豪情. All rights reserved.
//

import UIKit

class MLDSearchView: UIView,UITextFieldDelegate{
    //textfield 距右边的约束
    @IBOutlet weak var textFieldRightCons: NSLayoutConstraint!
    //取消按钮
    @IBOutlet weak var cancelButton: UIButton!
    //textfield
    @IBOutlet weak var textField: UITextField!
    
    class func SearchView() -> MLDSearchView {
        return NSBundle.mainBundle().loadNibNamed("MLDSearchView", owner: nil, options: nil).last as! MLDSearchView
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    override func awakeFromNib() {
//        textField.layer.borderWidth = 1
//        textField.layer.borderColor = UIColor.blueColor().CGColor
//        textField.layer.cornerRadius = 5
        
        
        textField.delegate = self
        
        let leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        
        leftView.size = CGSizeMake(height, height)
        
        leftView.contentMode = UIViewContentMode.Center
        
        textField.leftView = leftView
        
        textField.leftViewMode = UITextFieldViewMode.Always
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    //取消按钮点击事件
    @IBAction func cancelButtonClick(sender: UIButton) {
        endEditing(true)
        textFieldRightCons.constant = 0
        
        UIView.animateWithDuration(0.25) { () -> Void in
            self.layoutIfNeeded()
        }
    }
    //开始编辑
    func textFieldDidBeginEditing(textField: UITextField) {
       
        self.textFieldRightCons.constant = self.cancelButton.width
        
        UIView.animateWithDuration(0.25) { () -> Void in
            self.layoutIfNeeded()
        }
    }
    //结束编辑
    func textFieldDidEndEditing(textField: UITextField) {
       
    }
}
